package com.fastjrun.eladmin.itrms.service.impl;

import com.fastjrun.eladmin.itrms.common.exception.ServiceException;
import com.fastjrun.eladmin.itrms.dao.RmsSoftDao;
import com.fastjrun.eladmin.itrms.dto.SoftDTO;
import com.fastjrun.eladmin.itrms.dto.SoftQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsSoft;
import com.fastjrun.eladmin.itrms.entity.RmsSoftExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsSoftMapper;
import com.fastjrun.eladmin.itrms.service.RmsSoftControllerService;
import com.fastjrun.eladmin.itrms.utils.PageUtils;
import com.fastjrun.eladmin.itrms.vo.SoftVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("rmsSoftControllerService")
public class RmsSoftControllerServiceImpl extends BaseItrmsService implements RmsSoftControllerService {
    @Resource
    RmsSoftDao rmsSoftDao;
    @Resource
    RmsSoftMapper rmsSoftMapper;

    private RmsSoftExample retrive(SoftQueryDTO queryDTO) {
        log.debug("queryDTO={}", queryDTO);
        RmsSoftExample rmsSoftExample = new RmsSoftExample();
        RmsSoftExample.Criteria criteria = rmsSoftExample.createCriteria();
        if (queryDTO != null) {
            if (!StringUtils.isBlank(queryDTO.getSoftName())) {
                criteria.andSoftNameLike(queryDTO.getSoftName());
            }
        }
        return rmsSoftExample;
    }

    @Override
    public void export(HttpServletResponse response, SoftQueryDTO queryDTO) {
        RmsSoftExample rmsSoftExample = this.retrive(queryDTO);
        List<RmsSoft> all = rmsSoftDao.selectByExampleWithBLOBs(rmsSoftExample);
        List<SoftVO> projectVOS = this.rmsSoftMapper.toVO(all);
        List<Map<String, Object>> list = Lists.newArrayList();
        for (RmsSoft soft : all) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("编号", soft.getId());
            map.put("软件名称", soft.getSoftName());
            map.put("备注", soft.getRemark());
            map.put("负责用户ID", soft.getOwnerUserId());
            map.put("创建用户ID", soft.getCreateUserId());
            map.put("负责用户", soft.getOwnerUser());
            map.put("创建用户", soft.getCreateUser());
            map.put("创建时间", soft.getCreateDate());
            map.put("更新时间", soft.getUpdateDate());
            list.add(map);
        }
        try {
            FileUtil.downloadExcel(list, response);
        } catch (IOException e) {
            log.warn("", e);
            throw new ServiceException(e);
        }


    }

    @Override
    public PageResult<SoftVO> query(SoftQueryDTO queryDTO, Pageable pageable) {

        if (pageable == null) {
            pageable = PageRequest.of(PAGE_CURRENT, PAGE_SIZE);
        }


        PageUtils.startPage(pageable);

        RmsSoftExample rmsSoftExample = this.retrive(queryDTO);
        String orderClause = this.getOrderClause(pageable);
        if (StringUtils.isNotBlank(orderClause)) {
            rmsSoftExample.setOrderByClause(orderClause);
        }


        PageInfo<RmsSoft> pageInfo = new PageInfo<>(rmsSoftDao.selectByExampleWithBLOBs(rmsSoftExample));
        List<SoftVO> list = rmsSoftMapper.toVO(pageInfo.getList());

        PageResult<SoftVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void create(SoftDTO createDTO) {
        RmsSoft rmsSoft = this.rmsSoftMapper.toEntity(createDTO);
        rmsSoft.setCreateUser(SecurityUtils.getCurrentUsername());
        rmsSoft.setCreateUserId(SecurityUtils.getCurrentUserId());
        rmsSoft.setOwnerUser(sysUserDao.selectByPrimaryKey(createDTO.getOwnerUserId()).getUsername());
        rmsSoft.setCreateDate(new Date());
        rmsSoft.setUpdateDate(new Date());
        rmsSoftDao.insert(rmsSoft);

    }

    @Override
    public void update(SoftDTO updateDTO) {
        RmsSoft rmsSoft = this.rmsSoftMapper.toEntity(updateDTO);
        rmsSoft.setOwnerUser(sysUserDao.selectByPrimaryKey(updateDTO.getOwnerUserId()).getUsername());
        rmsSoft.setUpdateDate(new Date());
        rmsSoftDao.updateByPrimaryKeySelective(rmsSoft);
    }

    @Override
    public void delete(Long[] ids) {
        RmsSoftExample rmsSoftExample = new RmsSoftExample();
        rmsSoftExample.createCriteria().andIdIn(Arrays.asList(ids));
        rmsSoftDao.deleteByExample(rmsSoftExample);

    }
}
